# This file is part of BlackArch Linux ( https://www.blackarch.org/ ).
# See COPYING for license details.

pkgname=dnsgrep
pkgver=14.3f4fa7c
pkgrel=3
pkgdesc='A utility for quickly searching presorted DNS names.'
groups=('blackarch' 'blackarch-recon')
arch=('x86_64' 'aarch64')
url='https://github.com/erbbysam/DNSGrep'
license=('MIT')
depends=('glibc' 'go')
makedepends=('git')
source=("$pkgname::git+https://github.com/erbbysam/DNSGrep.git")
sha512sums=('SKIP')

pkgver() {
  cd $pkgname

  echo $(git rev-list --count HEAD).$(git rev-parse --short HEAD)
}

prepare() {
  cd $pkgname

  sed -i 's/github.com\/golang\/example/golang.org\/x\/example/' DNSBinarySearch/DNSBinarySearch.go
  sed -i "s/config.json/\/usr\/share\/dnsgrep\/config.json/" experimentalServer/experimentalServer.go
  # Horrible hack to fix https://github.com/erbbysam/DNSGrep/issues/3 and offer
  # package binaries. It only affects the build directory, so not a big deal
  cp -r /usr/lib/go "$srcdir/go"
  cd .. && cp -r $pkgname "$srcdir/go/src"
}

build() {
  cd $pkgname

  GOFLAGS="-trimpath -buildmode=pie -mod=readonly -modcacherw"

  GOROOT="$srcdir/go" GOPATH="$srcdir" go mod init go.mod
  GOROOT="$srcdir/go" GOPATH="$srcdir" go mod tidy
  GOROOT="$srcdir/go" GOPATH="$srcdir" go mod download
  GOROOT="$srcdir/go" GOPATH="$srcdir" go build \
    $GOFLAGS \
    -ldflags "-s -w" \
    -o $pkgname .

  GOROOT="$srcdir/go" GOPATH="$srcdir" go build \
    $GOFLAGS \
    -ldflags "-s -w" \
    -o $pkgname-server ./experimentalServer/
}

package() {
  cd $pkgname

  install -Dm 755 $pkgname $pkgname-server -t "$pkgdir/usr/bin"
  install -Dm 644 experimentalServer/config.json -t "$pkgdir/usr/share/$pkgname"
  install -Dm 644 -t "$pkgdir/usr/share/doc/$pkgname/" README.md
}

